﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;

public class naojizhuanwanManager : MonoBehaviour {
    public UILabel problme;
    public UILabel answer;
    public UILabel coinLabel;
    public UILabel levelLabel;
    public GameObject AnswerButton;
    public Dictionary<string, ConfigDataStyle> configDictionary = new Dictionary<string, ConfigDataStyle>();
    private int ConfigFirstId;
    private int ConfigLastId;
    private int GetIntId;
	// Use this for initialization
	void Start () {
        //PlayerPrefs.DeleteAll();
        ReadConfig();
	}
	
	// Update is called once per frame
	void Update () {
        coinLabel.text = PlayerPrefs.GetInt( "coin", 0 ).ToString();
    }
    public class ConfigDataStyle
    {
        public string problme;
        public string answer;
    }
    public void ReadConfig()
    {
        TextAsset binAsset = Resources.Load("config/naojinzhuanwan", typeof(TextAsset)) as TextAsset;
        //获取配置表的行
        string[] configdata = binAsset.text.Split('\n');

        foreach (var data in configdata)
        {
            ConfigDataStyle configdat = new ConfigDataStyle();
            string[] dath = data.ToString().Split(',');
            if (dath.Length >= 3)
            {
                string id = dath[0];
                configdat.problme = dath[1];
                configdat.answer = dath[2];
                configDictionary.Add(id, configdat);
            }
        }
        ConfigLastId = int.Parse(configdata[configdata.Length - 1].Split(',')[0]);  //最后一个ID
        ConfigFirstId = int.Parse(configdata[1].Split(',')[0]);  //第一个id
        GetIntId = PlayerPrefs.GetInt("njid", ConfigFirstId);
        Init(GetIntId.ToString());
    }

    public void Init(string i)
    {
        answer.gameObject.SetActive(false);
        problme.text = configDictionary[i].problme;
        answer.text = configDictionary[i].answer;
        AnswerButton.SetActive(true);
        coinLabel.text = PlayerPrefs.GetInt( "coin", 0 ).ToString();
        levelLabel.text = PlayerPrefs.GetInt( "njid", ConfigFirstId ).ToString() + "/" + ConfigLastId;
    }

    public void TopStroy()
    {
        int tap = PlayerPrefs.GetInt("njid");
        Debug.Log(tap);
        if (tap -ConfigFirstId> 0)
        {
            PlayerPrefs.SetInt("njid", PlayerPrefs.GetInt("njid",ConfigFirstId) - 1);
            Init(PlayerPrefs.GetInt("njid").ToString());
        }
    }
    public void DownStroy()
    {
        int Down = PlayerPrefs.GetInt("njid");
        Debug.Log(Down);
        if (Down + 1 <= ConfigLastId)
        {
            PlayerPrefs.SetInt("njid", PlayerPrefs.GetInt("njid",ConfigFirstId) + 1);
            Init(PlayerPrefs.GetInt("njid").ToString());
        }
    }

    public void OpenAnswer()
    {
        AnswerButton.SetActive(false);
        answer.gameObject.SetActive(true);
    }
}
